package com.esoon.service.impl;

import com.esoon.dao.CosTenantDao;
import com.esoon.dao.CsrCallLostDao;
import com.esoon.entity.CsrCallLost;
import com.esoon.entity.MyTenant;
import com.esoon.service.CsrCallLostService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ClassName CsrCallLostServiceImlp
 * @Description TODO
 * @Author 2020/11/13 9:33
 * @Version 1.0
 **/
@Service
public class CsrCallLostServiceImpl implements CsrCallLostService {
    private static final Logger logger;

    static {
        logger = LoggerFactory.getLogger(CsrCallLostServiceImpl.class);
    }
    @Resource
    private CsrCallLostDao csrCallLostDao;
    @Resource
    private CosTenantDao cosTenantDao;
    @Override
    public int insert(String clPhone ,String clSkill,String tenantCode) {
        try {
            //获取租户的相关信息
            MyTenant myTenant = cosTenantDao.selectByTenantCode(tenantCode);
            Integer ttId = myTenant.getTtId();
            String ttName = myTenant.getTtName();
            CsrCallLost csrCallLost = new CsrCallLost();
            csrCallLost.setClPhone(clPhone);
            csrCallLost.setClState("待回呼");
            csrCallLost.setClSkill(clSkill);
            csrCallLost.setCreateTime(new Date());
            csrCallLost.setTenantId(ttId);
            csrCallLost.setTenantName(ttName);
            int insert = csrCallLostDao.insert(csrCallLost);
            logger.info("插入成功，返回数据" + insert);
            return insert;
        }catch (Exception e){
            logger.error("插入失败，错误信息如下" + e.toString());
            return 500;
        }

    }
    //根据电话号码和租户信息，在数据库中查询对应信息
    //根据查询到的结果，返回对应的数据    当查询到的数据大于等于2 条的时候，返回1. 当查询到的数据小于2条是，返回0 ，当查询数据出错的时候，返回500
    @Override
    public String selectAbandonPhone(String startTime, String endTime, String phone,String tenantCode) {
        try{
            int i = csrCallLostDao.selectAbandonPhone(startTime, endTime, phone, tenantCode);
            logger.info("调用查询30分钟内连续两次放弃接口成功" + i);
            if (i >= 2){
                return "1";
            }else{
                return "0";
            }
        }catch (Exception e){
            logger.info("调用查询30分钟内连续两次放弃接口成功" + e.toString());
            return "500";
        }
    }
}
